#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int dice24[24][6] = {
        {3,1,0,5,4,2}, {1,2,0,5,3,4}, {2,4,0,5,1,3}, {4,3,0,5,2,1},
        {3,5,1,4,0,2}, {5,2,1,4,3,0}, {2,0,1,4,5,3}, {0,3,1,4,2,5},
        {0,1,2,3,4,5}, {1,5,2,3,0,4}, {5,4,2,3,1,0}, {4,0,2,3,5,1},
        {5,1,3,2,4,0}, {1,0,3,2,5,4}, {0,4,3,2,1,5}, {4,5,3,2,0,1},
        {3,0,4,1,5,2}, {0,2,4,1,3,5}, {2,5,4,1,0,3}, {5,3,4,1,2,0},
        {2,1,5,0,4,3}, {1,3,5,0,2,4}, {3,4,5,0,1,2}, {4,2,5,0,3,1},
};

#define MAXN 5

int N;
vector<string> names;
int dice[MAXN][6];
int rot[MAXN];
int ans;

int getID(char *buf) {
	int i;
	string name(buf);
	for (i=0; i<names.size(); i++) {
		if (names[i] == name) return i;
	}
	names.push_back(name);
	return i;
}

void check() {
	int colors[MAXN][6];
	for (int i=0; i<N; i++) {
		for (int j=0; j<6; j++) {
			colors[i][j] = dice[i][dice24[rot[i]][j]];
		}
	}
	int total = 0;
	for (int i=0; i<6; i++) {
		int cnt[MAXN*6] = {0};
		int max_face = 0;
		for (int j=0; j<N; j++) {
			max_face = max(max_face, ++cnt[colors[j][i]]);
		}
		total += N-max_face;
	}
	ans = min(ans, total);
}

void dfs(int n) {
	if (n >= N) {
		check();
		return;
	}
	for (int i=0; i<24; i++) {
		rot[n] = i;
		dfs(n+1);
	}
}

int main() {
	while (~scanf("%d", &N)) {
		if (N==0) break;
		names.clear();
		for (int i=0; i<N; i++) {
			for (int j=0; j<6; j++) {
				char buf[30];
				scanf("%s", buf);
				dice[i][j] = getID(buf);
			}
		}
		rot[0] = 2;
		ans = 4*6;
		dfs(1);
		printf("%d\n", ans);
	}
	return 0;
}
